package net.lmc.realtime.jtp.dim.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.configuration.Configuration;

import java.util.HashSet;
import java.util.Set;

public class DimDataFilterFunction extends RichFilterFunction<String> {

    private Set<String> dimSet;

    //数据操作类型
    private final Set<String> operateTypeSet = new HashSet<String>(){{
        add("insert");add("update");add("delete");
    }};

    public DimDataFilterFunction(Set<String> dimSet){
        this.dimSet = dimSet;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }

    @Override
    public boolean filter(String value) throws Exception {
        JSONObject jsonObject = JSON.parseObject(value);

        //获取表名称
        String tableName = jsonObject.getString("table_name");

        //获取操作类型
        String operateType = jsonObject.getString("operate_type");

        //判断进行过滤
        return dimSet.contains(tableName) && operateTypeSet.contains(operateType);
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
}
